package nju.mscs.data;

import java.util.Iterator;

import nju.mscs.data.object.Bill;
import nju.mscs.data.object.Database;
import nju.mscs.data.object.User;
import nju.mscs.data.object.VipConsumer;
import nju.mscs.data.object.VipConsumerUpdata;

public class Check {
	protected Database sumDB;
	public Check (Database sum){
		sumDB=sum;
	}
	public void trim(Database db){
		Iterator<VipConsumerUpdata> iv=db.getVipUpdata();
		while(iv.hasNext()){
			VipConsumerUpdata up=iv.next();
			switch(up.getType()){
			case VipConsumerUpdata.ADD:
				sumDB.addVipConsumer(up.getName(), up.getId(), up.isSex(), up.getTelephone(), up.getBirth());
				break;
			case VipConsumerUpdata.DELETE:
				sumDB.deleteVipConsumer(up.getId());
				break;
			case VipConsumerUpdata.MODIFY:
				VipConsumer vip=sumDB.searchVipConsumerById(up.getId());
				vip.addSumPoints(up.getSumPointChage());
				vip.addRestPoints(up.getRestPointChange());
				break;
			case VipConsumerUpdata.CHANGE:
				VipConsumer vi=sumDB.searchVipConsumerById(up.getId());
				vi.setBirth(up.getBirth());
				vi.setTelephone(up.getTelephone());
				vi.setSex(up.isSex());
				break;
			}
			Iterator<User> iu=db.getUserList();
			while(iu.hasNext()){
				User user=iu.next();
				if(sumDB.searchUserById(user.getId())==null)
					sumDB.addUser(user);
			}
		}
		Iterator<Bill> ib=db.getSaleRecord();
		while(ib.hasNext()){
			Bill bill=ib.next();
			if(sumDB.searchBill(bill.getId())==null)
				sumDB.addBill(bill);
		}
	}
	public Database copyDBFromSum(){
		return sumDB.copyDB();
	}
}

	